package cn.ad.exception;


import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/28.
 */
public class AdViewValidateException extends RuntimeException {

    /**
     * 平台代码: AdView平台代码为 2
     */
    public static final Integer PLATFORM_CODE=2;

    private Integer code;

    private String msg;

    public enum ErrorCode{

        ADVIEW_NOT_JSON(10001,"The request parameter is not a json object"),
        ADVIEW_INVALID_USERID_TOKEN(10002,"Invalid userId or token"),
        ADVIEW_INVALID_CHANNEL(10003,"Invalid channel"),
        ADVIEW_ADCHECK_FAIL(10004,"Ad check failed"),
        ADVIEW_ORIG_SAVE_FAIL(10005,"Creative save failed"),
        ADVIEW_AD_SAVE_FAIL(10006,"Ad save failed"),
        ADVIEW_MISSING_PARAM(11001,"Missing param"),
        ADVIEW_TYPE_MISMATCH(11002,"The parameter's type mismatch"),
        ADVIEW_INVALID_DATE(11003,"The date is invalid"),
        ADVIEW_STR_NULL(11004,"Character cannot be empty"),
        ADVIEW_INVALID_URL(11005,"The URL is invalid"),
        ADVIEW_NUM_INVALID(11006,"Please enter a valid number like 0 or 1"),
        ADVIEW_INVALID_NUM(11007,"Please enter a valid number like 1 to 20"),
        ADVIEW_INVALID_ID(11008,"Please enter a valid ID"),
        ADVIEW_IMAGES_SIZE(11009,"The size of image in URL dose not match your set size"),
        ADVIEW_IMAGES_URL_INVALID(11010,"The URL of image is invalid"),
        ADVIEW_ICONS_SIZE(11011,"The size of icons in URL dose not match your set size"),
        ADVIEW_ICONS_URL_INVALID(11012,"The URL of icon is invalid"),
        ADVIEW_INPUT_NUM_INVALID(11013,"Please enter a valid number like 1 to 3"),
        ADVIEW_VIDEO_RESOLUTION_INVALID(11014,"Please enter a valid video resolution"),
        ADVIEW_ADTYPE_INVALID(11015,"Please enter a valid adType like number -1 or 0 or 1"),
        ADVIEW_NATIVEPIC_IMAGE_SIZE(11101,"The image size of URL in nativePic is not in conformity with the setting."),
        ADVIEW_NATIVEPIC_IMAGE_URL_INVALID(11102,"The URL of image in nativePic is invalid");


        private Integer adViewCode;

        private String message;

        private static Map<Integer,String> mappings;


        ErrorCode(Integer adViewCode, String message) {
            this.adViewCode = adViewCode;
            this.message = message;
        }

        private synchronized static Map<Integer,String> getMappings(){
            if (mappings==null){
                mappings=new HashMap<>();
                for (ErrorCode code: ErrorCode.values()){
                    mappings.put(code.adViewCode,code.message);
                }
            }
            return mappings;
        }

        public static String forValue(int value) {
            return getMappings().get(value);
        }
    }


    public AdViewValidateException(Integer code, String msg) {
        super(msg);
        if (ErrorCode.forValue(code)!=null){
            this.msg = ErrorCode.forValue(code)+" "+msg;
        }else{
            this.msg=msg;
        }
        code= Integer.parseInt(PLATFORM_CODE+""+code);
        this.code = code;
    }


    public AdViewValidateException() {
    }

    public AdViewValidateException(String message, Throwable cause) {
        super(message, cause);
    }

    public AdViewValidateException(Throwable cause) {
        super(cause);
    }

    public AdViewValidateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
